<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\Article;

class CategoryController extends Controller
{
    // 分类页主页
    public function index()
    {
        // 所有分类
        $cates = Category::select('id', 'name')->withCount('articles')->get();
        $articles = Article::select()->orderBy('created_at', 'desc')->take(7)->get();
        return view('category', ['cates' => $cates, 'articles' => $articles]);
    }


    // 某个分类下的所有文章
    public function detail(Request $request, $id)
    {
        // 所有分类
        $cates = Category::select('id', 'name')->withCount('articles')->get();

        $articles = Article::where('category_id', $id)->get();
        return view('category', ['cates' => $cates, 'articles' => $articles]);
    }
}
